package io;

import java.io.File;
import java.util.Scanner;
public class IODemo13 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要搜索的文件名:");
        String fileName = scanner.nextLine();
        System.out.println("请输入要搜索的路径:");
        String rootName = scanner.nextLine();
        //判段路径合法性
        File rootPath = new File(rootName);
        if(!rootPath.isDirectory()) {
            System.out.println("输入的路径有误!");
            return;
        }
        scanDir(rootPath,fileName);
    }
    private static void scanDir(File rootPath,String fileName) {
        System.out.println("当前遍历到: " + rootPath.getAbsolutePath());

        File[] files = rootPath.listFiles();
        if(files == null) {
            return;
        }
        for(File f: files ) {
            if(f.isFile()) {
                if(f.getName().equals(fileName)) {
                    System.out.println("找到了: " + f.getAbsolutePath());
                }
            }else if(f.isDirectory()) {
                scanDir(f,fileName);
            }else {
                //暂时不用
            }
        }
    }
}
